package com.luych.toolbox.base.service.controller;

import com.luych.toolbox.base.service.entity.Letter;
import com.luych.toolbox.base.service.service.LetterService;
import com.luych.toolbox.common.feign.intf.base.service.LetterFeign;
import com.luych.toolbox.common.feign.param.base.service.LetterParam;
import com.luych.toolbox.common.feign.view.base.service.LetterView;
import com.luych.toolbox.common.page.CommonPageParam;
import com.luych.toolbox.common.page.CommonPageResult;
import com.luych.toolbox.common.page.CommonSort;
import com.luych.toolbox.common.page.PageResult;
import com.luych.toolbox.common.service.base.BaseController;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "站内信")
@RestController
@RequestMapping("/letter")
public class LetterController
        extends BaseController<LetterView, LetterParam, Letter>
        implements LetterFeign {

    private final LetterService letterService;

    @Autowired
    public LetterController(LetterService letterService) {
        super(letterService);
        this.letterService = letterService;
    }

    @Override
    public CommonPageResult<LetterView> pageByKeyword(long pageSize, long pageNumber, String sort, String order, String keyword, Boolean readed) {
        CommonPageParam commonPageParam = CommonPageParam.getInstance().setPageSize(pageSize).setPageNumber(pageNumber);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            commonPageParam.addSort(CommonSort.getInstance().setSort(sort).setOrder(order));
        }
        PageResult<LetterView> pageResult = letterService.pageByKeyword(commonPageParam, keyword, readed);
        return CommonPageResult.getInstance(pageResult);
    }

    @Override
    public void setReaded(String id) {
        letterService.setReaded(id);
    }

    @Override
    public void setUnread(String id) {
        letterService.setUnread(id);
    }

    @Override
    public int setReadedAll() {
        return letterService.setReadedAll();
    }

    @Override
    public int setUnreadAll() {
        return letterService.setUnreadAll();
    }

    @Override
    public int deleteReaded() {
        return letterService.deleteReaded();
    }

    @Override
    public int deleteUnread() {
        return letterService.deleteUnread();
    }

    @Override
    public int deleteAll() {
        return letterService.deleteAll();
    }
}
